import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class DeleteWin extends JFrame implements ActionListener{
    JButton exit,delete,confirm;
    String user;
    Connection con;
    Statement sql;
    ResultSet rs;
    JLabel label;
    JTextField field;
    JTable table;
    JScrollPane scrollPane;
    Object[][] info;
    Object column_name[]={"学号","姓名","成绩","家乡"};
    int NumOfRow;
    OperationWin operationWin;

    DeleteWin(String title,String user){
        setTitle(title);
        this.user=user;
        try{
            con= DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=Student","sa","061210");
            sql=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=sql.executeQuery("SELECT * FROM student");
            rs.last();
            NumOfRow=rs.getRow();
            info=new Object[NumOfRow][4];
            rs.beforeFirst();
            int i=0;
            while(rs.next())
            {
                info[i][0]=rs.getString(1);
                info[i][1]=rs.getString(2);
                info[i][2]=rs.getString(3);
                info[i][3]=rs.getString(4);
                i++;
            }
            con.close();
        }
        catch(SQLException e){
            JOptionPane.showMessageDialog(this,e,"WARNING",JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }

        table=new JTable(info,column_name);
        scrollPane=new JScrollPane(table);
        field=new JTextField(10);
        label=new JLabel("输入学号");

        exit=new JButton("退出");
        delete=new JButton("删除信息");
        confirm=new JButton("确定");
        exit.addActionListener(this);
        delete.addActionListener(this);
        confirm.addActionListener(this);

        setLayout(null);
        add(label);
        add(field);
        add(confirm);
        add(scrollPane);
        add(delete);
        add(exit);
        label.setBounds(110,20,100,40);
        field.setBounds(190,25,100,30);
        confirm.setBounds(330,25,80,30);
        scrollPane.setBounds(0,80,500,300);
        delete.setBounds(100,400,100,30);
        exit.setBounds(300,400,100,30);

        setBounds(700,400,500,500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==exit){
            if (JOptionPane.showConfirmDialog(this, "是否退出", "提示", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
                dispose();
                operationWin=new OperationWin("系统操作界面",user);
            }
        }
        else if(event.getSource()==confirm){
            String str=field.getText().trim();
            try{
                con= DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=Student","sa","061210");
                sql=con.createStatement();
                rs = sql.executeQuery("SELECT * FROM student WHERE Number ='"+str+"'");
                if(!rs.next())
                    JOptionPane.showMessageDialog(this,"学号不存在","提示",JOptionPane.PLAIN_MESSAGE);
                else{
                    info=new Object[1][4];
                    info[0][0]=rs.getString(1);
                    info[0][1]=rs.getString(2);
                    info[0][2]=rs.getString(3);
                    info[0][3]=rs.getString(4);
                    remove(scrollPane);
                    table=new JTable(info,column_name);
                    scrollPane=new JScrollPane(table);
                    add(scrollPane);
                    scrollPane.setBounds(0,80,500,300);
                    NumOfRow=1;
                    con.close();
                }
            }
            catch(SQLException e){
                JOptionPane.showMessageDialog(this,e,"WARNING",JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        }
        else if(event.getSource()==delete){
            if(NumOfRow!=1){
                JOptionPane.showMessageDialog(this,"请输入学号","HINT",JOptionPane.PLAIN_MESSAGE);
            }
            else{
                String str=field.getText().trim();
                if(JOptionPane.showConfirmDialog(this,"确定删除该信息？","HINT",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION) {
                    try {
                        con = DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=Student", "sa", "061210");
                        sql = con.createStatement();
                        sql.executeUpdate("DELETE FROM student WHERE Number='" + str + "'");
                        JOptionPane.showMessageDialog(this, "删除成功", "SUCCESS", JOptionPane.PLAIN_MESSAGE);
                        con.close();
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(this, e, "WARNING", JOptionPane.WARNING_MESSAGE);
                        System.exit(0);
                    }
                }
                try{
                    con= DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=Student","sa","061210");
                    sql=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                    rs=sql.executeQuery("SELECT * FROM student");
                    rs.last();
                    NumOfRow=rs.getRow();
                    info=new Object[NumOfRow][4];
                    rs.beforeFirst();
                    int i=0;
                    while(rs.next())
                    {
                        info[i][0]=rs.getString(1);
                        info[i][1]=rs.getString(2);
                        info[i][2]=rs.getString(3);
                        info[i][3]=rs.getString(4);
                        i++;
                    }
                    remove(scrollPane);
                    table=new JTable(info,column_name);
                    scrollPane=new JScrollPane(table);
                    add(scrollPane);
                    scrollPane.setBounds(0,80,500,300);
                    con.close();
                }
                catch(SQLException e){
                    JOptionPane.showMessageDialog(this,e,"WARNING",JOptionPane.WARNING_MESSAGE);
                    System.exit(0);
                }
            }
        }
    }
}
